1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #pragma warning(disable:4996) #include<cstdio> #include<vector> #include<cmath> #include<queue> #include<string.h> #include<algorithm> #include<list> #define INF 999999999999 #define ll long long using namespace std;
int n; int k, l;
int map[100][100];
deque<pair<int, int>> snake; queue<pair<int, int>> cmd; int direction = 0; int dx[4] = { 0,1,0,-1 }; int dy[4] = { 1,0,-1,0 };
int headx, heady; int t = 0;
bool inArea(int x, int y) { if (0 <= x&&x < n && 0 <= y&&y < n) return true; return false; }
int main() { scanf("%d %d", &n, &k); snake.push_back(make_pair(0, 0));
for (int i = 0; i < k; i++) { int a, b; scanf("%d %d", &a, &b); map[a - 1][b - 1] = 2; }
scanf("%d", &l); for (int i = 0; i < l; i++) { int t; char c; scanf("%d %c", &t, &c);
if (c == 'D') { cmd.push(make_pair(t, 1)); } else { cmd.push(make_pair(t, -1)); } }
while (true) {
t++; headx += dx[direction]; heady += dy[direction];
if (!inArea(headx, heady) || map[headx][heady] == 1) { printf("%d\n", t); return 0; } if (map[headx][heady] == 0) { map[snake.back().first][snake.back().second] = 0; snake.pop_back(); }
snake.push_front(make_pair(headx, heady)); map[headx][heady] = 1;
if (cmd.size()>0 && t == cmd.front().first) { if (direction + cmd.front().second < 0) direction = 3; else if (direction + cmd.front().second > 3) direction = 0; else direction += cmd.front().second; cmd.pop(); } } }
|